package com.gopay.bis.khpay.ebank.operation;

import java.math.BigDecimal;

import com.ecc.emp.data.KeyedCollection;
import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.khpay.ebank.KhPayMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.ICertHelperKhPay;
import com.sdb.payclient.core.PayclientInterfaceUtil;

/**
 * 平安银行跨行支付接口
 * 
 * @KhPayCertHelper.java
 * @author zhengxue
 * @2017-8-15 下午4:42:37 www.gopay.com.cn Inc.All rights reserved.
 */
public class KhPayCertHelper implements ICertHelperKhPay {
    private String encoding = "GBK";

    @Override
    public Object signData(BaseMessage bsaeMessage) throws GopayException {
        try {
            KhPayMessage message = (KhPayMessage) bsaeMessage;

            KeyedCollection inputOrig = new KeyedCollection("inputOrig");
            inputOrig.put("masterId", message.getMasterId());
            inputOrig.put("orderId", message.getBankOrderId());
            inputOrig.put("currency", message.getCurrency());
            inputOrig.put("amount", message.getAmount().toString());
            inputOrig.put("paydate", message.getOrderDate());
            inputOrig.put("objectName", message.getObjectName());
            inputOrig.put("validtime", "0"); // 订单有效期(秒)，0不生效
            inputOrig.put("remark", message.getRemark());
            inputOrig.put("plantId", message.getPlantId());
            inputOrig.put("plantBankId", message.getPlantBankId());
            String orig = ""; // 原始数据
            String sign = ""; // 签名数据

            PayclientInterfaceUtil util = new PayclientInterfaceUtil();
            KeyedCollection signDataput = new KeyedCollection("signDataput");
            signDataput = util.getSignData(inputOrig);

            orig = (String) signDataput.getDataValue("orig"); // 获取原始数据
            System.out.println("---原始数据---"+orig);
            sign = (String) signDataput.getDataValue("sign"); // 获取签名数据

            orig = PayclientInterfaceUtil.Base64Encode(orig, encoding); // 原始数据先做Base64Encode转码
            sign = PayclientInterfaceUtil.Base64Encode(sign, encoding); // 签名数据先做Base64Encode转码

            orig = java.net.URLEncoder.encode(orig, encoding); // Base64Encode转码后原始数据,再做URL转码
            sign = java.net.URLEncoder.encode(sign, encoding); // Base64Encode转码后签名数据,再做URL转码
            message.setPlainMessage(orig);
            message.setSignMessage(sign);
            BankLogUtils.printB2CSignPlainLog(message);
            BankLogUtils.printB2CSignSignatureLog(message, sign);
            return message;
        } catch (Exception e) {
            BankLogUtils.printB2CSignExceptionLog(bsaeMessage, e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6029);
        }
    }

    @Override
    public Object verify(BaseMessage bsaeMessage) throws GopayException {
        try {
            KhPayMessage message = (KhPayMessage) bsaeMessage;
            BankLogUtils.printB2CVerifyPlainLog(message);
            BankLogUtils.printB2CVerifySignatureLog(message, message.getSignMessage());

            PayclientInterfaceUtil util = new PayclientInterfaceUtil();
            KeyedCollection output = new KeyedCollection("output");
            String plain = message.getPlainMessage(); // 明文数据
            String sign = message.getSignMessage(); // 密文数据
            if (message.isFront()) {
                plain = java.net.URLDecoder.decode(plain, encoding);
                sign = java.net.URLDecoder.decode(sign, encoding);
            }

            plain = PayclientInterfaceUtil.Base64Decode(plain, encoding);
            sign = PayclientInterfaceUtil.Base64Decode(sign, encoding);
            System.out.println("---Base64Decode解码后的通知原始数据---"+plain); 
            System.out.println("---Base64Decode解码后的通知签名数据---"+sign);
            boolean result = util.verifyData(sign, plain);
            message.setVerifyResult(result);
            BankLogUtils.printB2CVerifyResultLog(message, result);

//            if (result) {
                output = util.parseOrigData(plain);
                message.setStatus((String) output.getDataValue("status"));
                message.setOrderDate((String) output.getDataValue("date"));
                message.setCharge(String.valueOf(output.getDataValue("charge")));
                message.setMasterId((String) output.getDataValue("masterId"));
                message.setBankOrderId((String) output.getDataValue("orderId"));
                message.setCurrency((String) output.getDataValue("currency"));
                message.setAmount(new BigDecimal(String.valueOf(output.getDataValue("amount"))));
                message.setPaydate((String) output.getDataValue("paydate"));
                message.setObjectName((String) output.getDataValue("objectName"));
                message.setValidtime(String.valueOf(output.getDataValue("validtime")));
                message.setRemark((String) output.getDataValue("remark"));
//            }
            return message;
        } catch (Exception e) {
            BankLogUtils.printB2CVerifyExceptionLog(bsaeMessage, e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6031);
        }
    }

    @Override
    public String sign(BaseMessage bsaeMessage) throws GopayException {
        // TODO Auto-generated method stub
        return null;
    }

}
